#!/bin/sh

if [ ! -f data/scripts/update_version ]; then
	echo "this script must be run from project root"
	exit 1
fi

LIBROXML_MAJOR=$(sed -n 's#.*MAJOR_VERSION.*, \(.*\))$#\1#p' configure.ac)
LIBROXML_MINOR=$(sed -n 's#.*MINOR_VERSION.*, \(.*\))$#\1#p' configure.ac)
LIBROXML_MICRO=$(sed -n 's#.*MICRO_VERSION.*, \(.*\))$#\1#p' configure.ac)
PREV_VERS=$LIBROXML_MAJOR.$LIBROXML_MINOR.$LIBROXML_MICRO

echo -n "previous version (detected=$PREV_VERS): "
read ANS
[ -z "$ANS" ] && PREV_VERS=$ANS
echo -n "new version: "
read ANS
if [ -z "$ANS" ]; then
	echo "version number is empty, abort."
	exit 1
fi
VERSION=$ANS

MAJOR=$(echo $VERSION | cut -d"." -f1)
MINOR=$(echo $VERSION | cut -d"." -f2)
MICRO=$(echo $VERSION | cut -d"." -f3)

DATE=$(LANG=C date +"%a, %d %b %Y %X %z")

# update version number in required files:
# configure.ac 'version=VERSION"
sed -i "s#\(.*MAJOR_VERSION.*, \)\(.*\))\$#\1$MAJOR)#" configure.ac
sed -i "s#\(.*MINOR_VERSION.*, \)\(.*\))\$#\1$MINOR)#" configure.ac
sed -i "s#\(.*MICRO_VERSION.*, \)\(.*\))\$#\1$MICRO)#" configure.ac

# debian/rules 'version=VERSION'
sed -i "s,\(version=\).*,\1$VERSION," debian/rules

# libroxml.spec 'version VERSION'
sed -i "s,\(version \).*,\1$VERSION," libroxml.spec

# CMakeLists.txt 'set (SOVERSION "2.0.2")'
sed -i "s,\(set (SOVERSION \"\).*\(\")\),\1$VERSION\2," CMakeLists.txt

# debian/changelog
echo "libroxml ($VERSION-1) unstable; urgency=low" > .debian.changelog
echo "" >> .debian.changelog
echo " -- Name <mail@libroxml.net>  $DATE" >> .debian.changelog
echo "" >> .debian.changelog
cat debian/changelog >> .debian.changelog
mv .debian.changelog debian/changelog

vi debian/changelog

